package cn.com.haopy.yue.ai.admin.web;

import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.validator.Order;
import cn.com.haopy.yue.ai.core.validator.Sort;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskCheckIndex;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSAdmin;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskCheckIndexService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/3/27 14:03
 */
@RestController
@RequestMapping("/admin/risk/check/index/result")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "AI指标相关")
public class AdminAiRiskCheckIndexController {
    @Autowired
    private AIRiskCheckIndexService checkIndexService;

    @RequiresPermissions("admin:risk:check:index:result:list")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI指标管理"}, button = "AI指标查询")
    @GetMapping("/list")
    @ApiOperation("列表")
    public Object list(@RequestParam(required = false) @ApiParam("结果类型") String resultType,
                       @RequestParam(required = false) @ApiParam("检查项") String checkIndexName,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        Page<AIRiskCheckIndex> checkIndexResultList = checkIndexService.querySelective(resultType, checkIndexName, page, limit, sort, order);
        return ResponseUtil.okPage(checkIndexResultList);
    }

    @RequiresPermissions("admin:risk:check:index:result:notPage")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI指标管理"}, button = "查询")
    @GetMapping("/notPage")
    @ApiOperation("不分页列表")
    public Object notPage() {
        List<AIRiskCheckIndex> checkIndexResultList = checkIndexService.list();
        return ResponseUtil.ok(checkIndexResultList);
    }

    @RequiresPermissions("admin:risk:check:index:result:create")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI指标管理"}, button = "AI指标新增")
    @PostMapping("/create")
    @ApiOperation("新增")
    @Transactional
    public Object create(@RequestBody AIRiskCheckIndex checkIndexResult) {
        Object error = validate(checkIndexResult);
        if (error != null) {
            return error;
        }
        Subject currentUser = SecurityUtils.getSubject();
        HOSAdmin admin = (HOSAdmin) currentUser.getPrincipal();
        checkIndexResult.setCreateBy(admin.getUsername());
        checkIndexResult.setUpdateBy(admin.getUsername());
        List<AIRiskCheckIndex> checkIndexResultList = checkIndexService.getByName(checkIndexResult.getCheckIndexName());
        if (!CollectionUtils.isEmpty(checkIndexResultList)) {
            return ResponseUtil.fail("指标已经存在");
        }
        boolean isSuccess = checkIndexService.save(checkIndexResult);
        if (!isSuccess) {
            return ResponseUtil.fail("添加失败");
        }
        return ResponseUtil.ok("添加成功");
    }

    @RequiresPermissions("admin:risk:check:index:result:update")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI指标管理"}, button = "AI指标修改")
    @PostMapping("/update")
    @ApiOperation("编辑")
    @Transactional
    public Object update(@RequestBody AIRiskCheckIndex checkIndexResult) {
        AIRiskCheckIndex checkIndexResultEntity = checkIndexService.getById(checkIndexResult.getId());
        if (Objects.isNull(checkIndexResultEntity)) {
            return ResponseUtil.fail("指标信息不存在");
        }

        // 指标唯一性校验
        if (!Objects.equals(checkIndexResult.getCheckIndexName(), checkIndexResultEntity.getCheckIndexName())) {
            List<AIRiskCheckIndex> checkIndexResultList = checkIndexService.getByName(checkIndexResult.getCheckIndexName());
            if (!CollectionUtils.isEmpty(checkIndexResultList)) {
                return ResponseUtil.fail("指标已经存在！");
            }
        }
        Subject currentUser = SecurityUtils.getSubject();
        HOSAdmin admin = (HOSAdmin) currentUser.getPrincipal();
        checkIndexResult.setUpdateBy(admin.getUsername());
        if (checkIndexService.updateById(checkIndexResult)) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @RequiresPermissions("admin:risk:check:index:result:detail")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI指标管理"}, button = "AI指标详情")
    @GetMapping("/detail")
    @ApiOperation("详情")
    @Transactional
    public Object detail(@RequestParam Integer id) {
        AIRiskCheckIndex entity = checkIndexService.getById(id);
        if (Objects.nonNull(entity)) {
            return ResponseUtil.ok(entity);
        } else {
            return ResponseUtil.fail("获取数据失败");
        }
    }

    @RequiresPermissions("admin:risk:check:index:result:delete")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI指标管理"}, button = "AI指标删除")
    @GetMapping("/delete")
    @ApiOperation("删除")
    @Transactional
    public Object delete(@RequestParam Integer id) {
        if (checkIndexService.removeById(id)) {
            return ResponseUtil.ok("删除成功");
        }
        return ResponseUtil.fail("删除数据失败");
    }

    private Object validate(AIRiskCheckIndex data) {
        if (!StringUtils.hasText(data.getCheckIndexName())) {
            return ResponseUtil.badArgumentValue();
        }
        return null;
    }
}
